package cn.gson.crmdemo.controller.order.controller;


import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.crmdemo.model.pojo.Contract;
import cn.gson.crmdemo.model.service.ContractService;
import cn.gson.crmdemo.model.vo.ContractCustomerVo;

@Controller
public class ContractController {
	@Autowired
	ContractService cs;
	
	/**
	 * 查询合同表
	 * @return
	 */
	@RequestMapping("contract-selectContract")
	@ResponseBody
	public Map<String,Object> contractList(@RequestParam("page")Integer pageNum,@RequestParam("pageSize")Integer pageSize) {
		Page<Object> page=PageHelper.startPage(pageNum, pageSize);
		JSONArray list = cs.selectContract();
		Map<String,Object> map=new HashMap<>();
		map.put("list", list);
		map.put("total", page.getTotal());
		return map;
	}

	
	
	/**
	 * 查询全部公司名称
	 * @return
	 */
	@RequestMapping("contract-selectAllcompany")
	@ResponseBody
	public JSONArray selectCompany() {
		return cs.selectCompany();
	}
	
	
	/**
	 * 模糊查询公司名称
	 * @return
	 */
	@RequestMapping("contract-selectcompany")
	@ResponseBody
	public Map<String,Object> mhselectCompany(@RequestParam("page")Integer pageNum,@RequestParam("pageSize")Integer pageSize,String a) {
		Page<Object> page=PageHelper.startPage(pageNum, pageSize);
		JSONArray list = cs.mhselectCompany("%"+a+"%");
		Map<String,Object> map=new HashMap<>();
		map.put("list", list);
		map.put("total", page.getTotal());
		return map;
	}
	
	
	/**
	 * 添加/修改合同表
	 * @param hetong
	 * @return
	 */
	@RequestMapping("add-contract")
	public String addContract(ContractCustomerVo hetong) {
		System.err.println(hetong);
		cs.addContract(hetong);
		//重定向到查询页面
		return "redirect:contract-selectContract?page=1&pageSize=4";
	}
	
	
	/**
	 * 删除合同表
	 * @param a
	 * @return
	 */
	@RequestMapping("delete-contract")
	public String deleteContract(int a) {
		System.err.println(a);
		cs.deleteContract(a);
		//重定向到查询页面
		return "redirect:contract-selectContract?page=1&pageSize=4";
	}
	
	
	/**
	 * 修改合同内容查询id
	 * @param a
	 * @return
	 */
	@RequestMapping("contract-updateselect")
	@ResponseBody
	public JSONArray updateselect(int a) {
		JSONArray list = cs.updateSelect(a);
		return list;
	}
	
	
	/**
	 * 查询合同状态
	 * @return
	 */
	@RequestMapping("contract-selectstate")
	@ResponseBody
	public JSONArray selectState(int a) {
		JSONArray list = cs.selectState(a);
		return list;
	}
}
